﻿using UnityEngine;
using System.Collections;
using System;
using System.Text;

public class Dialog  {
    private GameObject dialogGameObject;
    private Dialog() {
    }
    public void onClick(GameObject o) {
                hide();
                switch(o.name) {
                case "BtnOK":
                    Debug.Log("ok");
                if(onClickListener != null) {
                    onClickListener(o,true);
                }
                    break;
                case "BtnCancle":
                    onClickListener(o,false);
                break;
            }
        }
    public GameObject getRootGameObject() {
        return dialogGameObject; 
    }
    public delegate void DialogVoidDelegate(GameObject o,bool isOK);

    public DialogVoidDelegate onClickListener;

    public static Dialog create(string dialogGameObjectName) {
       Dialog mDialog = new Dialog();
       mDialog.dialogGameObject = GameObject.Find(dialogGameObjectName);
       mDialog.hide();
       return mDialog;
    }
    public static Dialog create(string dialogGameObjectName, DialogVoidDelegate onClick1) {
        Dialog mDialog =  create(dialogGameObjectName);
        if(mDialog.dialogGameObject == null ) {
            Debug.LogWarning("Can`t find "+ dialogGameObjectName);
            return mDialog;
        }
        GameObject OK = mDialog.dialogGameObject.transform.FindChild("BtnOK").gameObject;
        GameObject cancle = mDialog.dialogGameObject.transform.FindChild("BtnCancle").gameObject;
        UIEventListener.Get(OK).onClick = mDialog.onClick;
        UIEventListener.Get(cancle).onClick = mDialog.onClick;
        mDialog.onClickListener = onClick1;
        return mDialog;
    }
    public void setText(String text) {
        (GameObject.Find("Text")).GetComponent<UILabel>().text = text;
    }
    public void show() {
        if(dialogGameObject == null) {
            return;
        }
        dialogGameObject.SetActive(true);
    }

    public void hide() {
        if(dialogGameObject==null) {
         return;
        }
        dialogGameObject.SetActive(false);
    }
}
